package com.gxa.ssm251.controller.impl;

import com.alibaba.fastjson.JSON;
import com.gxa.ssm251.controller.UserController;
import com.gxa.ssm251.pojo.Menu;
import com.gxa.ssm251.pojo.Role;
import com.gxa.ssm251.pojo.User;
import com.gxa.ssm251.service.UserService;
import com.gxa.ssm251.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller("userController")
@RequestMapping("/user")
public class UserControllerImpl {

    //    @Autowired
    @Resource
    private UserService userService = null;

    @ResponseBody
    public User selectById(@RequestParam() Integer id) {
        User user = userService.selectById(16);
        ResultJson resultJson = new ResultJson();
        resultJson.setCode("1");
        resultJson.setData(user);
        String s = JSON.toJSONString(resultJson);
        return user;
    }

    @PostMapping("/login")
    @ResponseBody
    public List<Menu> login() {
        User user = userService.selectById(16);
        List<Role> roleList = user.getRoleList();
        List<Menu> menusInfo = new ArrayList<>();
        for (Role role : roleList) {
            List<Menu> menuList = role.getMenuList();
            List<Menu> menus1 = new ArrayList<>();
            int flag = 0;
            for (Menu menu : menuList) {
                Integer pid = menu.getPid();
                if (pid == flag) {
                    List<Menu> menuList1 = getMenuList(menuList, menu.getId());
                    menu.setMenuList(menuList1);
                    menusInfo.add(menu);
                }else {
                    continue;
                }
            }
        }
        System.out.println(menusInfo);
        return menusInfo;

    }

    public List<Menu> getMenuList(List<Menu> menuList, int flag) {
        List<Menu> menusInfo = new ArrayList<>();
        for (Menu menu : menuList) {
            Integer pid = menu.getPid();
            if (pid == flag) {
                List<Menu> menuList1 = getMenuList(menuList, menu.getId());
                menu.setMenuList(menuList1);
                menusInfo.add(menu);
            }else{
                continue;
            }
        }
        return menusInfo;
    }

}
